package com.yike.user.lkl.incoming;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.reflect.TypeToken;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.google.gson.Gson;
import com.yike.common.tool.StringUtil;
import com.yike.user.lkl.incoming.request.IncomingFiles;
import com.yike.user.lkl.incoming.request.MerchantRequest;
import com.yike.user.lkl.incoming.request.MerchantUpdateRequest;
import com.yike.user.lkl.incoming.response.FileResponse;
import com.yike.user.lkl.incoming.response.MerchantResponse;
import com.yike.user.lkl.incoming.response.MerchantUpdateResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author whc
 * @date 2025/5/8 10:00
 * 拉卡拉进件接口
 */

@Slf4j
@Component
public class IncomingApi extends IncomingCommon  {





    /**
     * @author whc
     * @date 2025/5/8 10:00
     * 上传文件
     */
    public FileResponse IncomingFilesPost(IncomingFiles incomingFiles) throws IOException {

        if(incomingFiles == null){
            throw new RuntimeException("参数错误");
        }
        if(ObjectUtils.isEmpty(incomingFiles.getFile())){
            throw new RuntimeException("文件不能为空");
        }
        if(StringUtil.isEmpty(incomingFiles.getImgType())){
            throw new RuntimeException("图片类型不能为空");
        }
        if(StringUtil.isEmpty(incomingFiles.getSourcechnl())){
            throw new RuntimeException("图片来源渠道不能为空");
        }

        String api = "/sit/htkregistration/file/upload";

        if (this.getProperties().getApiType().equals("prod")){
            api = "/registration/file/upload";
        }

        String result = this.sendPostRequestFile(api, incomingFiles);

        log.info("请求拉卡拉上传文件信息，,result:{}",JSONObject.toJSONString(result));

        if(StringUtil.isNotBlank(result)){
            return JSONObject.parseObject(result,FileResponse.class);
        }
        return null;

    }


    /**
     * @author whc
     * @date 2025/5/8 10:00
     * 商户进件接口
     */
    public MerchantResponse merchant(MerchantRequest merchantRequest) throws IOException {

        merchantRequest.setUserNo(Long.valueOf(getProperties().getUserNo()));
        paramCheck(merchantRequest);
        String api = "/sit/htkregistration/merchant";
        if (this.getProperties().getApiType().equals("prod")){
            api = "/registration/merchant";
        }
        log.info("请求进件相关请求参数，api:{},body:{}",api,JSONObject.toJSONString(merchantRequest));

        String result = this.sendPostRequestJson(api, JSONObject.toJSONString(merchantRequest));
        return JSONObject.parseObject(result,MerchantResponse.class);
    }


    /**
     * @author whc
     * @date 2025/5/8 10:00
     * 商户基本信息变更接口
     */
    public MerchantUpdateResponse merchantUpdate(MerchantUpdateRequest request) throws IOException {
        if(StringUtil.isEmpty(request.getCustomerNo())){
            throw new RuntimeException("商户编号不能为空");
        }

        String api = "/sit/htkmerchants/channel/customer/update/basic";
        if (this.getProperties().getApiType().equals("prod")){
            api = "/htkmerchants/channel/customer/update/basic";
        }
        String result = this.sendPostRequestLoginJson(api, JSONObject.toJSONString(request));

        return JSONObject.parseObject(result,MerchantUpdateResponse.class);
    }



    /**
     * @author whc
     * @date 2025/5/8 10:00
     * 商户结算信息变更
     */
    public MerchantUpdateResponse merchantSettleUpdate(MerchantRequest merchantRequest,String customerNo) throws IOException {

        Map<String ,Object> request = new HashMap<>();
        request.put("customerNo",customerNo);
        if(StringUtil.isNotBlank(merchantRequest.getAccountType())){
            request.put("accountKind",merchantRequest.getAccountType());
        }
        if(StringUtil.isNotBlank(merchantRequest.getAccountName())){
            request.put("accountName",merchantRequest.getAccountName());
        }
        if(StringUtil.isNotBlank(merchantRequest.getAccountNo())){
            request.put("accountNo",merchantRequest.getAccountNo());
        }
        if(StringUtil.isNotBlank(merchantRequest.getOpenningBankName())){
            request.put("bankName",merchantRequest.getOpenningBankName());
        }
        if(StringUtil.isNotBlank(merchantRequest.getOpenningBankCode())){
            request.put("bankNo",merchantRequest.getOpenningBankCode());
        }
        if(StringUtil.isNotBlank(merchantRequest.getClearingBankCode())){
            request.put("clearingBankNo",merchantRequest.getClearingBankCode());
        }
        if(StringUtil.isNotBlank(merchantRequest.getAccountIdCard())){
            request.put("identityNo",merchantRequest.getAccountIdCard());
        }
        if(ObjectUtils.isNotEmpty(merchantRequest.getIsLegalPerson())){
            request.put("isLegalPerson",merchantRequest.getIsLegalPerson());
        }
        if(StringUtil.isNotBlank(merchantRequest.getSettleCityCode())){
            request.put("settleCityCode",merchantRequest.getSettleCityCode());
        }
        if(StringUtil.isNotBlank(merchantRequest.getSettleCityName())){
            request.put("settleCityName",merchantRequest.getSettleCityName());
        }
        if(StringUtil.isNotBlank(merchantRequest.getSettleProvinceCode())){
            request.put("settleProvinceCode",merchantRequest.getSettleProvinceCode());
        }
        if(StringUtil.isNotBlank(merchantRequest.getSettleProvinceName())){
            request.put("settleProvinceName",merchantRequest.getSettleProvinceName());
        }

        String api = "/sit/htkmerchants/channel/customer/update/settle/" + customerNo;
        if (this.getProperties().getApiType().equals("prod")){
            api = "/htkmerchants/channel/customer/update/settle/" + customerNo;
        }
        String result = this.sendPostRequestLoginJson(api, JSONObject.toJSONString(request));

        return JSONObject.parseObject(result,MerchantUpdateResponse.class);
    }


    /**
     * @author whc
     * @date 2025/5/8 10:00
     * 商户费率信息变更
     */
    public MerchantUpdateResponse merchantFeeUpdate(MerchantRequest merchantRequest,String customerNo) throws IOException {

        Map<String ,Object> request = new HashMap<>();
        request.put("customerNo",customerNo);
        request.put("productCode","产品类型");
        request.put("settleType",merchantRequest.getSettleType());
        request.put("withdrawalType",merchantRequest.getBizContent().getWithdrawalType());
        request.put("settlementType",merchantRequest.getSettlementType());
        request.put("regularSettlementTime",merchantRequest.getRegularSettlementTime());
        request.put("fees",merchantRequest.getBizContent().getFees());



        String api = "/sit/htkmerchants/channel/customer/update/fee/" + customerNo;
        if (this.getProperties().getApiType().equals("prod")){
            api = "/htkmerchants/channel/customer/update/fee/" + customerNo;
        }
        String result = this.sendPostRequestLoginJson(api, JSONObject.toJSONString(request));

        return JSONObject.parseObject(result,MerchantUpdateResponse.class);
    }


    /**
     * @author whc
     * @date 2025/5/8 10:00
     * 获取地址信息
     */
    public List<Map<String, Object>> getAreaInfo(String code) throws IOException {

        String api = "/sit/htkregistration/organization/" + code;
        if (this.getProperties().getApiType().equals("prod")){
            api = "/registration/merchant" + code;
        }

        String result = this.sendGetRequest(api, null);
        Gson gson = new Gson();
        Type type = new TypeToken<List<Map<String, Object>>>(){}.getType();
        List<Map<String, Object>> list = gson.fromJson(result, type);
        return list;

    }


    /**
     * @author whc
     * @date 2025/5/8 10:00
     * 获取商户信息
     */
    public Map<String,Object> getMerchantInfo(String customerNo) throws Exception {

        Map<String ,Object> map = new HashMap<>();
        map.put("customerNo",customerNo); //内部商户号
        String api = "/sit/htkmerchants/open/merchant/info";
        if (this.getProperties().getApiType().equals("prod")){
            api = "/htkmerchants/open/merchant/info";
        }
        String reqData = LklRSAUtil.encrypt(JSON.toJSONString(map)); //加密
        Map<String ,Object> maps = new HashMap<>();
        maps.put("data",reqData);
        String result = this.sendPostRequestJsonByUrlThirdParty(api, JSON.toJSONString(maps));


        Map<String,Object> resultMap = JSONObject.parseObject(result);
        if(resultMap.containsKey("code") && resultMap.get("code").equals("000000")){
            String dataInfo = LklRSAUtil.decrypt((String) resultMap.get("data")); //解密
            Map<String,Object> data = JSONObject.parseObject(dataInfo);
            return data;
        }
        return null;
    }

    public void   paramCheck(MerchantRequest request) throws IOException {
        if (request == null){
            throw new RuntimeException("参数错误");
        }

        if (StringUtil.isEmpty(request.getUserNo())){
            throw new RuntimeException("商户归属用户信息不能为空");
        }

        if (StringUtil.isEmpty(request.getEmail())){
            throw new RuntimeException("商户邮箱不能为空");
        }
        if (StringUtil.isEmpty(request.getMerRegName())){
            throw new RuntimeException("商户注册名称不能为空");
        }
        if (StringUtil.isEmpty(request.getMerType())){
            throw new RuntimeException("商户类型不能为空");
        }
        if (StringUtil.isEmpty(request.getMerName())){
            throw new RuntimeException("商户名称(经营名称)不能为空");
        }
        if (StringUtil.isEmpty(request.getMerAddr())){
            throw new RuntimeException("商户详细地址不能为空");
        }
        if (StringUtil.isEmpty(request.getProvinceCode())){
            throw new RuntimeException("商户所在省代码不能为空");
        }
        if (StringUtil.isEmpty(request.getCityCode())){
            throw new RuntimeException("商户所在市代码不能为空");
        }

        if (StringUtil.isEmpty(request.getCountyCode())){
            throw new RuntimeException("商户所在区县代码不能为空");
        }

        if(!StringUtil.equals("TP_PERSONAL",request.getMerType())){
            if (StringUtil.isEmpty(request.getLicenseNo())){
                throw new RuntimeException("商户营业执照编号不能为空");
            }
            if (StringUtil.isEmpty(request.getLicenseDtStart())){
                throw new RuntimeException("商户营业执照开始日期不能为空");
            }
            if (StringUtil.isEmpty(request.getLicenseDtEnd())){
                throw new RuntimeException("商户营业执照结束日期不能为空");
            }
        }
        if (StringUtil.isEmpty(request.getLatitude())){
            throw new RuntimeException("进件所在地址经度不能为空");
        }
        if (StringUtil.isEmpty(request.getLongtude())){
            throw new RuntimeException("进件所在地址纬度不能为空");
        }
        if (StringUtil.isEmpty(request.getSource())){
            request.setSource("H5");
//            throw new RuntimeException("商户来源不能为空");
        }
        if (StringUtil.isEmpty(request.getBusinessContent())){
            throw new RuntimeException("商户经营内容不能为空");
        }


        if (StringUtil.isEmpty(request.getLarName())){
            throw new RuntimeException("商户法人姓名不能为空");
        }
        if (StringUtil.isEmpty(request.getLarIdType())){
            throw new RuntimeException("商户法⼈证件类型不能为空");
        }
        if (StringUtil.isEmpty(request.getLarIdCard())){
            throw new RuntimeException("商户法人证件号码不能为空");
        }
        if (StringUtil.isEmpty(request.getLarIdCardStart())){
            throw new RuntimeException("商户法人证件开始日期不能为空");
        }
        if (StringUtil.isEmpty(request.getLarIdCardEnd())){
            throw new RuntimeException("商户法人证件结束日期不能为空");
        }
        if (StringUtil.isEmpty(request.getContactMobile())){
            throw new RuntimeException("商户联系⼈⼿机号码不能为空");
        }
        if (StringUtil.isEmpty(request.getContactName())){
            throw new RuntimeException("商户联系⼈姓名不能为空");
        }
        if (StringUtil.isEmpty(request.getOpenningBankCode())){
            throw new RuntimeException("结算账户开户行号");
        }
        if (StringUtil.isEmpty(request.getOpenningBankName())){
            throw new RuntimeException("结算账户开户行名称不能为空");
        }
        if (StringUtil.isEmpty(request.getClearingBankCode())){
            throw new RuntimeException("结算账户清算⾏号不能为空");
        }
        if (StringUtil.isEmpty(request.getSettleProvinceCode())){
            throw new RuntimeException("结算信息省份代码不能为空");
        }
        if (StringUtil.isEmpty(request.getSettleProvinceName())){
            throw new RuntimeException("结算信息省份名称不能为空");
        }
        if (StringUtil.isEmpty(request.getSettleCityCode())){
            throw new RuntimeException("结算信息城市代码不能为空");
        }
        if (StringUtil.isEmpty(request.getSettleCityName())){
            throw new RuntimeException("结算信息城市名称不能为空");
        }



        if (StringUtil.isEmpty(request.getAccountNo())){
            throw new RuntimeException("结算人银行卡号不能为空");
        }
        if (StringUtil.isEmpty(request.getAccountName())){
            throw new RuntimeException("结算账户名称不能为空");
        }
        if (StringUtil.isEmpty(request.getAccountType())){
            throw new RuntimeException(" 结算账户类型 不能为空");
        }

        if (StringUtil.isEmpty(request.getAccountIdCard())){
            throw new RuntimeException("结算⼈证件号码不能为空");
        }
        if (StringUtil.isEmpty(request.getSettleType())){
            throw new RuntimeException("结算类型不能为空");
        }

        if (ObjectUtils.isEmpty(request.getAttchments())){
            throw new RuntimeException("附件信息不能为空");
        }
        if (ObjectUtils.isEmpty(request.getSettlementType())){
            throw new RuntimeException("结算方式不能为空");
        }
    }



}
